import java.util.List;

import algorithms.PolicyIteration;
import algorithms.QLearning;
import algorithms.ValueIteration;
import framework.State;

public class Executor {

	public static void main(String[] args) {
		/*State.generateAllStates();
		System.out.println("--States:");
		System.out.println("--\n");
		System.out.println(State.visualStates());
		System.out.println("--Performing value iteration...");
		System.out.println("--\n");
		ValueIteration.iterate();
		System.out.println("Value iteration policy:");
		System.out.println(ValueIteration.printPolicy());
		System.out.println("--Path taken with value iteration:");
		System.out.println("--\n");
		showPath();

		State.generateAllStates(); //Reset utilities etc
		System.out.println("--Performing policy iteration...");
		System.out.println("--\n");
		PolicyIteration.init();
		PolicyIteration.iterate();
		System.out.println("--Policy:");
		System.out.println("--\n");
		System.out.println(PolicyIteration.printPolicy());
		System.out.println("--Path taken with policy iteration:");
		System.out.println("--\n");
		showPath();*/
		
		State.generateAllStates(); //Reset utilities etc
		System.out.println("--Performing Q-Learning...");
		System.out.println("--\n");
		QLearning.init();
		QLearning.iterate();
		System.out.println("--Policy:");
		System.out.println("--\n");
		System.out.println(QLearning.printPolicy());
		System.out.println("--Path taken with Q-Learning:");
		System.out.println("--\n");
		showPath();
		
	}

	private final static String initialState = "0012", endState = "2212";

	private static void showPath() {
		double[] currState = State.allStates.get(initialState);
		State.toVisualString(currState,true);
		String curStateString = initialState;
		//
		List<String> nextStates = State.getPossibleTransitions(currState);
		while (!curStateString.equals(endState)) {
			double maxUtil = -10000;
			double[] maxState = null;
			//
			for (String s : nextStates) {
				//
				currState = State.allStates.get(s);
				curStateString = State.toString(currState);
				if (currState[4] > maxUtil || curStateString.equals(endState)) {
					maxUtil = currState[4];
					maxState = currState;
				}
			}
			currState = maxState;
			System.out.println(State.toVisualString(currState,true));
			nextStates = State.getPossibleTransitions(currState);
			curStateString = State.toString(currState);
		}
	}
}
